
capture program drop density_data
program define density_data
global bin=10
global binname=$bin
global data_in=20
global data_out=1180 
global range               "RC>=float(${data_in}) & RC<=float(${data_out})"
global range_rv            "incbin${binname}>= float(${data_in}+${bin}) & incbin${binname}<=float(${data_out}+${bin})"

use "${processed_data}\trading_rights_asif.dta"      if  ${firm}==1 & ${condition}  &  ${year_condition}  & ${range} , clear

gen capital_norm=RC  //registered capital; unit 10K RMB
gen tinctobin= capital_norm/${bin}
gen incbinaux=floor(tinctobin) 
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux
keep if $range_rv   


preserve
collapse (count) numbin${binname}=year,  by(incbin${binname}) 
qui save "${density_data_dir}\density_${firm_name}_${name}_${year_name}_bin${binname}_in${data_in}_out${data_out}.dta",replace
restore


gen random = runiform() 
sort incbin${binname} random 
by incbin${binname}: gen grouping = ceil(5 * _n/_N) 
foreach num in 1 2 3 4 5 {
preserve
keep if grouping==`num'
collapse (count) numbin${binname} =year ,  by(incbin${binname})
qui save "${density_mse_dir}\density_${firm_name}_${name}_${year_name}_`num'_bin${binname}_in${data_in}_out${data_out}.dta", replace
restore

preserve 
keep if grouping!=`num'
collapse (count) numbin${binname} =id ,  by(incbin${binname})
qui save "${density_mse_dir}\density_${firm_name}_${name}_${year_name}_exclude_`num'_bin${binname}_in${data_in}_out${data_out}.dta",replace
restore
}

end 



capture program drop density_fig_c300
program define density_fig_c300
global bin=10
global binname=$bin
global zoom_in_c300=80        
global zoom_out_c300=470
global range_c300   "incbin${binname}>=float(${zoom_in_c300}) & incbin${binname}<=float(${zoom_out_c300})"


use "${density_data_dir}\density_${firm_name}_${name}_${year_name}_bin${binname}_in${data_in}_out${data_out}.dta", clear  
keep if $range_c300

#delimit;
twoway (connected numbin${binname} incbin${binname}, sort clcolor(black) mcolor(black) msymbol(o) msize(large)),
xlabel(80  (20)  470, labsize(small))	
yscale(r(0))  ylabel(#5) 
legend(label(1 "observed density") size(small))
xline(300, lcolor(grey) lwidth(med) lpattern(shortdash) )	
xline(100 200 400, lcolor(grey) lwidth(thin) lpattern(dash) )	
xtitle(Registred Capital(unit:10,000 RMB), size(medsmall)  ) xtitle(, alignment(top)) 						
ytitle(Density, size(medsmall)   margin(medium))  
title("Density distribution of Registered Capital for", justification(center) color(black) size(medsmall)) 
subtitle( "${firm_name} Firms in ${location} ${year_note}"  "${cutoff_note}",  justification(center) color(black) size(small))
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
note( "Note: bin=${bin}; Data: ASIF.",  size(small))	 
;		
graph export "${density_graph_dir}/rawfig_density_${firm_name}_${name}_${year_name}_c300_bin${binname}_in${zoom_in_c300}_out${zoom_out_c300}.pdf", replace;
#delimit cr 

end



capture program drop density_fig_c200
program define density_fig_c200
global bin=10
global binname=$bin
global zoom_in_c200=80  
global zoom_out_c200=260 
global numerate_c200=20
global range_c200   "incbin${binname}>=float(${zoom_in_c200}) & incbin${binname}<=float(${zoom_out_c200})"

use "${density_data_dir}\density_${firm_name}_${name}_${year_name}_bin${binname}_in${data_in}_out${data_out}.dta", clear  
keep if $range_c200

#delimit;
twoway (connected numbin${binname} incbin${binname}, sort clcolor(green) mcolor(green) msymbol(o) msize(large)),
xlabel($zoom_in_c200  ($numerate_c200 )  $zoom_out_c200 , labsize(small))	
yscale(r(0))  ylabel(#5) 
legend(label(1 "observed density") size(small))
xline(200, lcolor(black) lwidth(med) )	
xtitle(Registred Capital(unit:10,000 RMB), size(medsmall)  ) xtitle(, alignment(top)) 						
ytitle(Density, size(medsmall)   margin(medium))  
title("Density distribution of Registered Capital for", justification(center) color(black) size(medsmall)) 
subtitle( "${firm_name} Firms in ${location} ${year_note}"  "${cutoff_note}",  justification(center) color(black) size(small))
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
note( "Note: bin=${bin}; Data: ASIF.",  size(small))	 
;		
graph export "${density_graph_dir}/rawfig_density_${firm_name}_${name}_${year_name}_c200_bin${binname}_in${zoom_in_c200}_out${zoom_out_c200}.pdf", replace;
#delimit cr

end



capture program drop density_fig_c100
program define density_fig_c100
global bin=10
global binname=$bin
global zoom_in_c100=60  
global zoom_out_c100=160 
global numerate_c100=20
global range_c100   "incbin${binname}>=float(${zoom_in_c100}) & incbin${binname}<=float(${zoom_out_c100})"

use "${density_data_dir}\density_${firm_name}_${name}_${year_name}_bin${binname}_in${data_in}_out${data_out}.dta", clear  
keep if $range_c100

#delimit;
twoway (connected numbin${binname} incbin${binname}, sort clcolor(green) mcolor(green) msymbol(o) msize(large)),
xlabel($zoom_in_c100  ($numerate_c100 )  $zoom_out_c100 , labsize(small))	
yscale(r(0))  ylabel(#5) 
legend(label(1 "observed density") size(small))
xline(100, lcolor(black) lwidth(med) )	
xtitle(Registred Capital(unit:10,000 RMB), size(medsmall)  ) xtitle(, alignment(top)) 						
ytitle(Density, size(medsmall)   margin(medium))  
title("Density distribution of Registered Capital for", justification(center) color(black) size(medsmall)) 
subtitle( "${firm_name} Firms in ${location} ${year_note}"  "${cutoff_note}",  justification(center) color(black) size(small))
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
note( "Note: bin=${bin}; Data: ASIF.",  size(small)) 
;		
graph export "${density_graph_dir}/rawfig_density_${firm_name}_${name}_${year_name}_c100_bin${binname}_in${zoom_in_c100}_out${zoom_out_c100}.pdf", replace;
#delimit cr

end





